Imports SQLUtil.SQL
Imports System.Windows.Forms
Public Class clsSupport

#Region "Basic"
    Public Shared Function GetWordCount(ByVal cString As String, ByVal cDelimiter As Char) As Object
        Dim num As Integer = 0
        Dim length As Long = cString.Length
        Dim num3 As Long = 0
        If (cString.Chars(0) <> cDelimiter) Then
            num3 = (num3 + 1)
        End If
        Dim num4 As Integer = (num + 1)
        Dim num5 As Integer = CInt((length - 1))
        num = 0
        Do While (((num4 >> &H1F) Xor num) <= ((num4 >> &H1F) Xor num5))
            If (cString.Chars(num) = cDelimiter) Then
                Do
                    num += 1
                    If (num >= length) Then
                        Exit Do
                    End If
                    If (cString.Chars(num) <> cDelimiter) Then
                        num3 = (num3 + 1)
                        Exit Do
                    End If
                Loop While (1 <> 0)
            End If
            num = (num + num4)
        Loop
        Return num3
    End Function

    Public Shared Function GetWordNum(ByVal cString As String, ByVal nWordPosition As Integer, ByVal cDelimiter As Char) As String
        cString = Trim(cString) + cDelimiter
        Dim num As Integer = At(cDelimiter, cString, (nWordPosition - 1))
        Dim num2 As Integer = At(cDelimiter, cString, nWordPosition)
        Return Strings.Mid(cString, (num + 1), ((num2 - 1) - num))
    End Function
    Public Shared Function At(ByVal cSearchFor As String, ByVal cSearchIn As String, ByVal nOccurence As Integer) As Integer
        Return _At(cSearchFor, cSearchIn, nOccurence, 1)
    End Function
    Private Shared Function _At(ByVal cSearchFor As String, ByVal cSearchIn As String, ByVal nOccurence As Integer, ByVal nMode As Integer) As Integer
        Dim num2 As Integer = 0
        Dim num3 As Integer = 0
        Dim startIndex As Integer = 0
        If (nMode = 1) Then
            startIndex = 0
        Else
            startIndex = cSearchIn.Length
        End If
        Dim num5 As Integer = (num2 + 1)
        Dim num6 As Integer = nOccurence
        num2 = 1
        Do While (((num5 >> &H1F) Xor num2) <= ((num5 >> &H1F) Xor num6))
            If (nMode = 1) Then
                startIndex = cSearchIn.IndexOf(cSearchFor, startIndex)
            Else
                startIndex = cSearchIn.LastIndexOf(cSearchFor, startIndex)
            End If
            If (startIndex < 0) Then
                Exit Do
            End If
            num3 += 1
            If (num3 = nOccurence) Then
                Return (startIndex + 1)
            End If
            If (nMode = 1) Then
                startIndex += 1
            Else
                startIndex -= 1
            End If
            num2 = (num2 + num5)
        Loop
        Return 0
    End Function
#End Region

#Region "MenuItem"
    Public Class appMenuItem
        Inherits System.Windows.Forms.MenuItem
        Private cId As String, cIconFile As String
        Private cFont As Font
        Private cIcon As Icon
        Private cBackgroundColor As Color = SystemColors.Highlight
        Private cControlColor As Color = SystemColors.Control
        Public strImageDir As String = ""

        Sub New()
            Dim strMenuFont As String
            strMenuFont = "Microsoft Sans Serif"
            cFont = New Font(strMenuFont, 8)
        End Sub

        Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)
            MyBase.OnMeasureItem(e)
            Dim itemSize As SizeF
            itemSize = e.Graphics.MeasureString(Me.Text, cFont)
            e.ItemWidth = itemSize.Width + 16
            e.ItemHeight = 22
        End Sub

        Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
            MyBase.OnDrawItem(e)
            Dim strIconFile As String
            If Me.Icon <> "" Then
                strIconFile = strImageDir + Me.Icon
                cIcon = New Icon(strIconFile)
            End If
            Dim br As Brush
            Dim rect As Rectangle = e.Bounds
            rect.X += 22
            If CBool(e.State And System.Windows.Forms.DrawItemState.Selected) Then
                br = New System.Drawing.Drawing2D.LinearGradientBrush(rect, cBackgroundColor, cControlColor, 0)
            Else
                br = SystemBrushes.Control
            End If
            e.Graphics.FillRectangle(br, e.Bounds)
            If Not cIcon Is Nothing Then
                Dim rt As New Rectangle(e.Bounds.Left + 2, e.Bounds.Top + 2, 16, 16)
                e.Graphics.DrawIcon(cIcon, rt)
            End If
            br = New SolidBrush(e.ForeColor)
            Dim strFormat As StringFormat = New StringFormat
            strFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show
            e.Graphics.DrawString(GetHotKeysString(), cFont, br, e.Bounds.Left + 2 + 16 + 5, e.Bounds.Top + 2, strFormat)
        End Sub

        Private Function GetHotKeysString()
            Dim s As String = MyBase.Text
            If ShowShortcut And Shortcut <> Windows.Forms.Shortcut.None Then
                Dim k As Keys = CType(MyBase.Shortcut, Keys)
                s &= System.ComponentModel.TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
            End If
            Return s
        End Function

        Public Property Id() As String
            Get
                Return cId
            End Get
            Set(ByVal Value As String)
                cId = Value
            End Set
        End Property

        Private cExe As String
        Public Property Exe() As String
            Get
                Return cExe
            End Get
            Set(ByVal Value As String)
                cExe = Value
            End Set
        End Property

        Private cEx As String
        Public Property Ex() As String
            Get
                Return cEx
            End Get
            Set(ByVal Value As String)
                cEx = Value
            End Set
        End Property

        Public Property Icon() As String
            Get
                Return cIconFile
            End Get
            Set(ByVal Value As String)
                cIconFile = Value
            End Set
        End Property
    End Class
#End Region

#Region "TreeNode"
    Private Class xTreeNode
        Inherits TreeNode
        Private dr As DataRow

        Public Property xDataRow() As DataRow
            Get
                Return dr
            End Get
            Set(ByVal Value As DataRow)
                dr = Value
            End Set
        End Property
    End Class
#End Region
#Region "TreeView"
    Private Class clsdirlistbox
        Inherits TreeView
        Public InstantiatedBy As String
        Public strQuery = "menu_id, menu_id0, bar, bar2 AS tag, exe, expl_icon"
        Public sMenuKey = "1=1"
        Public sysConn As SqlClient.SqlConnection = SQLConnect("")

        Enum imageIndexEnum
            OpenFolder = 0
            ClosedFolder = 1
            SelectedOpenFolder = 0
            SelectedClosedFolder = 1
        End Enum

        Dim images As New imageIndexEnum

        Public Function IsInstantiatedBy() As String
            Return InstantiatedBy
        End Function

        Public Sub New()
            MyBase.New()
            Me.HideSelection = False
        End Sub

        Public Sub Open(ByVal instantiator As String, ByVal cMenuID As String)
            Dim ds As New DataSet, strSQL As String, i As Integer
            If InstantiatedBy <> "" Then Exit Sub
            InstantiatedBy = instantiator
            Nodes.Clear()
            strSQL = "SELECT " + strQuery + " FROM command WHERE (" + sMenuKey + ") AND LEN(RTRIM(menu_id)) = 8 AND menu_id0 = '" + Trim(cMenuID) + "' ORDER BY Menu_id"
            SQLRetrieve(sysConn, strSQL, "Command", ds)
            Windows.Forms.Cursor.Current = Cursors.WaitCursor
            BeginUpdate()
            For i = 0 To ds.Tables("command").Rows.Count - 1
                With ds.Tables("command").Rows(i)
                    Dim xNode As xTreeNode
                    xNode = New xTreeNode
                    xNode.Text = Trim(.Item("Bar"))
                    xNode.ImageIndex = imageIndexEnum.ClosedFolder
                    xNode.SelectedImageIndex = xNode.ImageIndex
                    xNode.xDataRow = ds.Tables("command").Rows(i)
                    Nodes.Add(xNode)
                    AddPlaceHolders(xNode)
                End With
            Next
            Windows.Forms.Cursor.Current = Cursors.Default
            EndUpdate()
            Me.Select()
            If Nodes.Count > 0 Then
                SelectedNode = Nodes(0)
            End If
            ds = Nothing
            Return
        End Sub

        Public Sub Close(ByVal instantiator As String)
            If instantiator <> InstantiatedBy Then Exit Sub
            InstantiatedBy = ""
        End Sub

        Private Sub AddPlaceHolders(ByVal aNode As xTreeNode)
            Dim ds As New DataSet, strSQL As String, i As Integer
            strSQL = "SELECT " + strQuery + " FROM command WHERE (" + sMenuKey + ") AND menu_id0 = '" + Trim(aNode.xDataRow.Item("menu_id")) + "' ORDER BY Menu_id"
            SQLRetrieve(sysConn, strSQL, "Command", ds)
            For i = 0 To ds.Tables("command").Rows.Count - 1
                With ds.Tables("command").Rows(i)
                    If Microsoft.VisualBasic.Right(.Item("menu_id"), 2) = "00" Then
                        Dim xNode As xTreeNode
                        xNode = New xTreeNode
                        xNode.Text = Trim(.Item("Bar"))
                        xNode.ImageIndex = imageIndexEnum.ClosedFolder
                        xNode.SelectedImageIndex = xNode.ImageIndex
                        xNode.xDataRow = ds.Tables("command").Rows(i)
                        aNode.Nodes.Add(xNode)
                        AddPlaceHolders(xNode)
                    End If
                End With
            Next
            ds = Nothing
        End Sub

        Private Sub clsDirListBoxBeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles MyBase.BeforeExpand
            If e.Node.ImageIndex = imageIndexEnum.ClosedFolder Then
                e.Node.ImageIndex = imageIndexEnum.OpenFolder
                e.Node.SelectedImageIndex = imageIndexEnum.SelectedOpenFolder
            End If
        End Sub

        Private Sub clsDirListBeforeCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles MyBase.BeforeCollapse
            If e.Node.ImageIndex = imageIndexEnum.OpenFolder Then
                e.Node.ImageIndex = imageIndexEnum.ClosedFolder
                e.Node.SelectedImageIndex = imageIndexEnum.SelectedClosedFolder
            End If
        End Sub
    End Class
#End Region
End Class
